# _*_ coding: utf-8 _*_



# expressao lambda e map copia a lista uma sim outra nao maisuscula e minusculas
"""
def f(x):
	if len(x) % 2 -1:
	
		return x.lower()
	else:
		return x.upper()
		
expressao = lambda x: x.lower() if len(x) % 2 -1 else x.upper()

lista = ['Lisboa', 'Caracas', 'Madrid', 'Londres', 'Berlin', 'Paris']

lista_out1 = map(expressao, lista)
lista_out2 = map(f, lista)

print lista_out1
print lista_out2
"""
# calcular a media
"""
d1 ={'Programacao 1' : 15, 
	 'Programacao 2' : 13,
	 'Matematica'    : 12,
	 'Linguagens Programacao' : 17}
	 
d1['Algebra']= 13

media = 0.0



"""
"""
# para saber as vezes q aparece uma letra
# contagem de letras indice e repeticao 

s = "Mas que belo dia"

x = 'q'

if x in s: print " a letra 'q' encontra-se em s"

t = " em Amstterdao"

print s + t

print 3 * t

print s[5], s[5:8], s[5:12:2]

print len(t) 
print len(s)

print min(s)
print  max(t)

print "indice de 'q' =", s.index('q')

print "vezes em que aparece 'm' =", t.count('m')
"""

#particao de strings com espaco ''
"""
s1 = "Anda um espectro pela Europa"

i = s1.find('ctro', 3, 20)

print s1[i:i+5]

print s1.strip('\a')

t = s1.partition(' ')

print t

l="""
"""
Alma minha gentil, que te partiste

Alma minha gentil, que te partiste

Tao cedo desta vida, descontente

repousa la no Ceu eternamente

E viva eu ca na terra sempre triste.
"""
""".splitlines()

print l
"""


# calculo raiz quadrada
"""
import math

x = 23.4

s1 = "raiz quadrada"

y = 255

s2 = "Temos {:^40} de x = {valor: 2.4f}".format(s1,
	  valor = x)

s2 +=" e y = Ox{yy:X}".format(yy=y)

s2 += "\nA percentagem = {0:2.1%}".format(34 /100.0)

print s2

"""

#Leitura ficheiros
"""
# da me aqui um erro ao ler a primeira parte a meio
f = open("teste.txt", "w")

s = "treta"

for k in range(10): f.write(s); f.write("\n")

f.close

f = open("teste.txt", "r")

f.seek(5)

print f.tell()

x = f.read(10)

print x

c = 0

for line in f:
	
	print "{0}:" .format(c), line
	
	c += 1
	
f.close()

with open("teste.txt", "r") as f:
	
	x = f.readline()
	
	if x ==  'treta\n': print "voila"
"""
# class teste pickle
"""
import pickle

class Teste:
	
	def __init__(self, x):
		
		self.x = x
		
obj = Teste(49)

f = open("teste.dat", "w")

p = pickle.dump(obj, f)

f.close()

del obj

obj = pickle.load(open("teste.dat", "r"))

print obj.x
"""

########DA me ERRO
"""
from mmap import mmap,ACCESS_READ

from xlrd import open_workbook

print open_workbook('simple.xls')

with open('simple.xls', 'rb') as f:
	
	print open_workbook(
	
		file_contents=mmap(f.fileno(), 0,access= ACCESS_READ)
		)
aString = open('simple.xls', 'rb').read
print open_workbook(file_contents=aString)
"""

######## DAME ERRO
"""
from xlrd import open_workbook

wb = open_workbook('simple.xls')

for s in wb.sheets():
	
	print 'Folha:', s.name
	
	print "linhas = ", s.nrows, "colunas = ", s.ncols
	
	for row in range(s.nrows):
		values = []
		for col in range(s.ncols):
			values.append(s.cell(row,col).value)
		print ' , '.join(values)
	print
"""

"""
import sqlite3

conexao = sqlite3.connect('teste.db')

c = conexao.cursor

c.execute('DROP TABLE IF EXISTS disc')

c.execute('CREATE TABLE disc(nome text, nota number)')

conexao.commit()

notas = [('P1', 15), ('P2', 15),('LP',19)]

c.executemany('INSERT INTO disc VALUES (?,?)', notas)

conexao.commit()

c.execute("SELECT * FROM disc WHERE nota > 16")

print "uma ...", c.fectchone()

k =  0 ; soma = 0.0

for linha in c.execute("SELECT * FROM disc"):
	
	print linha; soma += linha [1]; k +=1
	
print "media = {:2.2f}".format(soma/k)

conexao.close()

"""
import sys

try:
	f = open('ficha.txt')
	s = f.readline()
	i = int(s.strip())
except IOErr as e:
	print "I/O error({0}): {1}".format(e.errno, e.strerror)
except ValueError:
	print "Could not convert data to an integer."
except:
	print "Unexpected error:", sys.exc_info()[0]
	raise
